<?php
namespace app\wework\home;

use app\common\controller\Common;
use src\api\src\CorpApi;
use src\callback\WXBizMsgCrypt;

/**
 * 企业微信回调
 * @author king
 *
 */
class Callback extends Common
{
	// 企业ID
	protected $corpid = null;

	// 应用ID
	protected $agentid = null;

	// 应用密钥
	protected $secret = null;

	protected $token = null;

	protected $encoding_aes_key = null;

	/**
	 * @var CorpApi
	 */
	protected $client = null;

	/**
	 * @var WXBizMsgCrypt
	 */
	protected $wxcpt = null;

	protected $logName = '';

	public function initialize() {
		$this->corpid = config('wework.corpid');
		$this->agentid = intval(config('wework.agentid'));
		$this->secret = config('wework.secret');
		$this->token = config('wework.token');	 // 接收消息服务器配置
		$this->encoding_aes_key = config('wework.encoding_aes_key'); // 接收消息服务器配置

		$this->client = new CorpApi($this->corpid, $this->agentid, $this->secret);
		$this->wxcpt = new WXBizMsgCrypt($this->token, $this->encoding_aes_key, $this->corpid);
	}

    public function index()
    {
    	$this->logName = 'Wework_Callback';
    	$get = input('get.');
    	$post = file_get_contents("php://input");

    	addLog(['get' => $get, 'post' => $post], $this->logName);

    	$msgSignature = arrayGet($get, 'msg_signature');
    	$timestamp = arrayGet($get, 'timestamp');
    	$nonce = arrayGet($get, 'nonce');
    	$echostr = arrayGet($get, 'echostr');

    	// 验证URL有效性
    	if ($echostr != '') {
	    	$sEchoStr = "";
	    	$errCode = $this->wxcpt->VerifyURL($msgSignature, $timestamp, $nonce, $echostr, $sEchoStr);
	    	if ($errCode != 0) {
	    		addLog('URL验证失败：'.$this->wxcpt->ErrorMsg($errCode), $this->logName);
	    		echo $this->wxcpt->ErrorMsg($errCode);
	    		exit;
	    	}

	    	exit($sEchoStr);
    	}

    	// 解密消息
    	$body = '';
    	try {
    		$res = $this->wxcpt->DecryptMsg($msgSignature, $timestamp, $nonce, $post, $body);
    		if ($res != 0) {
    			addLog($res . $this->wxcpt->ErrorMsg($res), $this->logName);
    			exit($res . $this->wxcpt->ErrorMsg($res));
    		}
    	} catch (\Exception $e) {
    		addLog($e->getMessage(), $this->logName);
    		exit($e->getMessage());
    	}

    	$msgType = arrayGet($body, 'MsgType');
    	$toUserName = arrayGet($body, 'ToUserName');
    	$fromUserName = arrayGet($body, 'FromUserName');
    	$this->wxcpt->setReplyUser($toUserName, $fromUserName);

    	addLog(['body' => $body], $this->logName);
    	switch ($msgType) {
    		case 'text':
    			return controller('wework/Text', 'home')->index($body, $this->wxcpt);
    			break;
    		case 'image':
    			break;
    		case 'voice':
    			break;
    		case 'video':
    			break;
    		case 'location':
    			break;
    		case 'link':
    			break;
    		case 'event':
    			return $this->_doEvent($body);
    			break;
    		default:
    			addLog('unkown msgType', $this->logName);
    	}
    	$sReplyMsg = json_encode($body, JSON_UNESCAPED_UNICODE);
    	return $this->wxcpt->replyText($sReplyMsg);
    }

    private function _doEvent($body) {
    	$event = arrayGet($body, 'Event');
    	switch ($event) {
    		case 'subscribe':	// 关注
    			break;
    		case 'unsubscribe':	// 取消关注
    			break;
    		case 'enter_agent':	// 进入应用
    			break;
    		case 'LOCATION':	// 上报地理位置
    			break;
    		case 'batch_job_result':	// 异步任务完成事件推送
    			break;
    		case 'click':	// 点击菜单拉取消息的事件推送
    			break;
    		case 'view':	// 点击菜单跳转链接的事件推送
    			break;
    		case 'scancode_push':	// 扫码推事件的事件推送
    			break;
    		case 'scancode_waitmsg':	// 扫码推事件且弹出“消息接收中”提示框的事件推送
    			return controller('wework/ScancodeWaitmsg', 'home')->index($body, $this->wxcpt);
    			break;
    		case 'pic_sysphoto':	// 弹出系统拍照发图的事件推送
    			break;
    		case 'pic_photo_or_album':	// 弹出拍照或者相册发图的事件推送
    			break;
    		case 'pic_weixin':	// 弹出微信相册发图器的事件推送
    			break;
    		case 'location_select':	// 弹出地理位置选择器的事件推送
    			break;
    		case 'taskcard_click':	// 任务卡片事件推送
    			break;
    		default:
    			addLog('unkown event ' . $event, $this->logName);
    	}
    	$sReplyMsg = json_encode($body, JSON_UNESCAPED_UNICODE);
    	return $this->wxcpt->replyText($sReplyMsg);
    }
}